﻿using MT.CustomUserControl.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MT.CustomUserControl.Views.UserControls
{
    /// <summary>
    /// PasswordDefaultUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class PasswordDefaultUserControl : UserControl
    {
        /// <summary>
        /// 是否显示锁图标的依赖属性
        /// </summary>
        public static readonly DependencyProperty IsShowEyeProperty = DependencyProperty.RegisterAttached("IsShowEye", typeof(Visibility), typeof(PasswordDefaultUserControl),
              new PropertyMetadata(Visibility.Visible, new PropertyChangedCallback(OnPropertyChanged)));

        public Visibility IsShowEye
        {
            get { return (Visibility)GetValue(IsShowEyeProperty); }
            set { SetValue(IsShowEyeProperty, value); }
        }

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordDefaultUserControl pb = (d as PasswordDefaultUserControl);
            //if (pb.IsShowLock)
            //{
            //    pb.GridColumn_Border_PasswordIcon.Width = new GridLength(40);
            //    pb.Border_PasswordIcon.Visibility = Visibility.Visible;
            //}
            //else
            //{
            //    pb.GridColumn_Border_PasswordIcon.Width = new GridLength(2);
            //    pb.Border_PasswordIcon.Visibility = Visibility.Collapsed;
            //}
        }

        public PasswordDefaultUserControl()
        {
            InitializeComponent();
        }

        public void InitPassword()
        {
            PasswordBox_Password.Password = string.Empty;
        }

        private void Btn_OpenCloseEye_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ChangePasswordTextBoxAndPasswordBoxVisibility();
        }

        private void Btn_OpenCloseEye_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ChangePasswordTextBoxAndPasswordBoxVisibility();
        }

        private void PasswordBox_Password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Window parentWindow = Window.GetWindow(this);
                (this.DataContext as PasswordUcViewModel)?.RaiseEnterInPasswordEvent(parentWindow);
            }
        }

        private void ChangePasswordTextBoxAndPasswordBoxVisibility()
        {
            if (TextBox_Password.Visibility == Visibility.Collapsed)
                TextBox_Password.Visibility = Visibility.Visible;
            else
                TextBox_Password.Visibility = Visibility.Collapsed;

            if (PasswordBox_Password.Visibility == Visibility.Collapsed)
                PasswordBox_Password.Visibility = Visibility.Visible;
            else
                PasswordBox_Password.Visibility = Visibility.Collapsed;
        }
    }
}
